/*
 * @Author: shaohh // 公共的loading方法
 * @Date: 2022-10-24 16:36:38
 * @Last Modified by: shaohh
 * @Last Modified time: 2023-01-12 09:28:34
 */

import { Loading } from 'element-ui'

const loadingEntity = {
  uuid: {},
  target: { }
}

export function show(loadingTarget, uuid) {
  if (!loadingTarget) {
    return
  }
  if (loadingTarget === 1) {
    handleLoading()
    return
  }
  const elArr = document.querySelectorAll(loadingTarget)
  elArr.forEach(el => {
    handleLoading(el)
  })
  function handleLoading(el) {
    let customClass
    const options = {
      fullscreen: true,
      lock: true,
      spinner: 'el-icon-loading',
      customClass,
      target: el,
      background: 'rgba(255, 255, 255, 0.7)'
    }
    const loadingService = Loading.service(options)
    let fontSize = 16
    if (el) {
      const clientRect = el.getBoundingClientRect()
      fontSize = clientRect.height > 100 ? 40 : 16
    } else {
      fontSize = 40
    }
    const icon = loadingService.$el.children[0]
    icon.style.fontSize = `${fontSize}px`

    if (!uuid) {
      if (loadingEntity.target[loadingTarget]) {
        loadingEntity.target[loadingTarget].push(loadingService)
      } else {
        loadingEntity.target[loadingTarget] = [loadingService]
      }
    } else {
      loadingEntity.uuid[uuid] = loadingService
    }
  }
}

export function cancel(key) {
  if (loadingEntity.uuid[key]) {
    loadingEntity.uuid[key].close()
    loadingEntity.uuid[key] = null
  } else if (loadingEntity.target[key]) {
    loadingEntity.target[key].forEach(loading => {
      loading.close()
    })
    loadingEntity.target[key] = null
  }
}

function cancelAll() {

}
